package net.juantxu.pentaho.launcher;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import net.juantxu.pentaho.launcher.utilidades.UnZip;

import org.apache.log4j.Logger;

public class Descargador {
	static Logger log = Logger.getLogger(DescargaEInstalaPentahoApp.class);
	
	public void descargaWin(String APP) {
		log.info("descargando aplicación Win de pentaho" + APP);
		String[] arr = APP.split("/");
		String miApp = arr[arr.length - 1];
		log.info("descargando  " + APP + "en " + miApp);
		descarga(APP, "pentahoApps/" + miApp);
		log.info("descargado " + APP + " en pentahoApps/" + miApp);
		log.info("Descomprimiendo... pentahoApps/" + miApp);
		new UnZip().unZip("pentahoApps/" + miApp);

	}
	

	public void descargaNix(String APP) {

		log.info("descargando aplicación de pentaho: " + APP);
		String[] arr = APP.split("/");
		String miApp = arr[arr.length - 1];
		//miApp.replaceAll("*", "");
		log.info("descargando  " + APP + " en " + miApp);
		descarga(APP, "pentahoApps/" + miApp);
		unzipNix("pentahoApps/" + miApp);
	}

	public static void unzipNix(String archivo) {
		String str[] = archivo.split("/");
		File miPath = new File(str[0]);
		log.debug("Ejecutando en :" + miPath.getAbsolutePath());
		Runtime r = Runtime.getRuntime();
		if (archivo.contains(".zip")) {
			new UnZip().unZip(archivo);
		} else {

			try {
				r.exec("tar -xzf " + str[1], null, miPath);
				Thread.sleep(3000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public static void descarga(String origen, String destino) {
		log.debug("comenzando la descarga");
		URL url; // represents the location of the file we want to dl.
		URLConnection con; // represents a connection to the url we want to dl.
		DataInputStream dis; // input stream that will read data from the file.
		FileOutputStream fos; // used to write data from inut stream to file.
		byte[] fileData; // byte aray used to hold data from downloaded file.
		try {
			url = new URL(origen);
			con = url.openConnection(); // open the url connection.
			dis = new DataInputStream(con.getInputStream()); // get a data
																// stream from
																// the url
																// connection.
			fileData = new byte[con.getContentLength()]; // determine how many
															// byes the file
															// size is and make
															// array big enough
															// to hold the data
			for (int x = 0; x < fileData.length; x++) { // fill byte array with
														// bytes from the data
														// input stream
				fileData[x] = dis.readByte();
			}
			dis.close(); // close the data input stream
			fos = new FileOutputStream(new File(destino)); // create an object
															// representing the
															// file we want to
															// save
			fos.write(fileData); // write out the file we want to save.
			fos.close(); // close the output stream writer
		} catch (MalformedURLException m) {
			System.out.println(m);
		} catch (IOException io) {
			System.out.println(io);
		}

		log.debug("Descarga finalizada");
	}
}
